package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.config.BaseContext;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @PostMapping("sendMsg")
    public R<String> sendMsg(@RequestBody Map<String,String> map, HttpSession session){

//        Integer code = ValidateCodeUtils.generateValidateCode(4);
        String code = "1234";
        log.info("创建出来验证码:{}",code);
        String phone = map.get("phone");
//        SMSUtils.sendMessage();

        //        查看redis中是否有验证码
        String codeInRedis = (String) redisTemplate.opsForValue().get(phone);
        if (StringUtils.isNotEmpty(codeInRedis)){
            return R.error("请等待验证码失效,再来获取");
        }
//        将验证码存入session中
//         session.setAttribute(phone,code);

//        将验证码存入redis中
          redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

         return R.success("获取验证码成功");
    }

    @PostMapping("/login")
    public R<String> login(@RequestBody Map<String,String> map,HttpSession session){
        String phone = map.get("phone");
        String code = map.get("code");

//        从session中获取验证码
//        String sendMsgCode = (String) session.getAttribute(phone);

//        从redis中获取验证码
        String sendMsgCode = (String) redisTemplate.opsForValue().get(phone);
        if (sendMsgCode == null){
            R.error("请先获取验证码");
        }
        if (!sendMsgCode.equals(code)){
            return R.error("登录失败");
        }
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,phone);
        User user = userService.getOne(lqw);
        if (user == null){
            user = new User();
            user.setPhone(phone);
            userService.save(user);
        }else{
            Integer status = user.getStatus();
            if (status == 0){
                R.error("用户已禁用");
            }
        }

//        将验证码从session中移除
//        session.removeAttribute(phone);

//        将验证码从redis中移除
          redisTemplate.delete(phone);

        session.setAttribute("user",user.getId());
        return R.success("登录成功");
    }

    @PostMapping("/loginout")
    public R loginOut(HttpSession session){
        session.removeAttribute("user");
        return R.success("退出成功");
    }
}
